package com.controller;

import com.domain.UserTestEntity;
import com.dto.JsonResponse;
import com.service.UserTestService;
import com.support.UserSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表简化 前端控制器
 * </p>
 *
 * @author sly
 * @since 2024-01-10
 */
@RestController
@RequestMapping("/userTest")
public class UserTestController {

    @Autowired
    private UserTestService userTestService;

    @Autowired
    private UserSupport userSupport;

    /**
     * 注册
     */
    @PostMapping("/register")
    public JsonResponse<String> register(@RequestBody UserTestEntity user) throws Exception {
        userTestService.register(user);
        return JsonResponse.success();
    }

    /**
     * 用户登录，返回令牌，令牌在用户使用其他功能时派的上用场
     */
    @PostMapping("/login")
    public JsonResponse<String> login(@RequestBody UserTestEntity user) throws Exception {
        String token = userTestService.login(user);
        return new JsonResponse<>(token);
    }

    /**
     * 获取当前用户详细信息
     */
    @GetMapping("/getUserInfo")
    public JsonResponse<UserTestEntity> getUserInfo() {
        Long userId = userSupport.getCurrentUserId();
        UserTestEntity user = userTestService.getUserInfo(userId);
        return new JsonResponse<>(user);
    }

    /**
     * 获取用户的配置信息
     */
    @GetMapping("/getUserDetail")
    public JsonResponse<UserTestEntity> getUserDetail(Long userId) {
        UserTestEntity user = userTestService.getUserDetail(userId, userSupport.getCurrentUserId());
        return new JsonResponse<>(user);
    }

    /**
     * 修改当前用户信息
     */
    @PostMapping("/updateUser")
    public JsonResponse<String> updateUser(@RequestBody UserTestEntity userTestEntity) {
        Long userId = userSupport.getCurrentUserId();
        userTestEntity.setId(userId);
        userTestService.updateUser(userTestEntity);
        return JsonResponse.success();
    }
}
